import contextvars
import random
import string

# 使用contextvars定义请求的上下文变量，用于记录请求ID，在RecordRequestMiddleware中设置
REQUEST_CONTEXT = {
    "request_id": contextvars.ContextVar("request_id", default=None),
}


def gen_random_key_list(length: int = 64) -> list:
    chars = list()
    chars.extend(string.ascii_letters)
    chars.extend(string.digits)

    random_key = list()
    if length < 1:
        length = 64
    for i in range(length):
        random_key.append(random.choice(chars))
    return random_key


def gen_random_key_string(length: int = 64) -> str:
    return "".join(gen_random_key_list(length))
